/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.unifor.redes1.odirabel;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author pedro
 */
public final class ConfigProperties {

    private Properties parameters;
    private File arq;

    private static ConfigProperties me;

    private ConfigProperties() throws IOException{
        constructor();
    }

    public static ConfigProperties getInstance() throws IOException
    {
        if(me == null){
            me = new ConfigProperties();
        }
        return me;
    }

    private void constructor() throws IOException{
        String file;
        file = "jlim.properties";
        String path = System.getProperty("user.home") + "/" + file;
        arq = new File(path);
        if (!arq.exists()) {
            try {
                arq.createNewFile();
            } catch (IOException ex) {
                throw ex;
            }
        }

        try {
            parameters = new Properties();
            parameters.load(new FileInputStream(arq));

        } catch (IOException ex) {
            throw ex;
        }
        if (parameters.isEmpty()) {
            parameters.put("ipservidor", "192.168.254.1");
            parameters.put("portaservidor", "5499");
            parameters.put("portacliente", "5477");
        }
        try {

            parameters.store(new FileOutputStream(arq), "Init");
        } catch (IOException ex) {
            throw ex;
        }
    }

    public String getProperty(String prop) {
        return parameters.getProperty(prop);
    }

    public void setProperty(String prop, String value) {
        parameters.setProperty(prop, value);

    }

    public void save() throws IOException {
        try {
            parameters.store(new FileOutputStream(arq), "Update");
        } catch (IOException ex) {
            throw ex;
        }
    }
}